# -*- coding: utf-8 -*-
# @time: 2024/6/6 15:41
# @file: IVnan_pct
# @author: tyshixi08

from get_data.IVdelta_mid import *

# 获取全部iv数据
def get_IV_all(code = code_ls(), sta_date = month_ls()[0] + '-01', end_date = month_ls()[-1] + '-01', flag=0):
    df = get_IV(code, sta_date, end_date, flag)
    return df

# 获取无效值占比
def NaN_pect(code = code_ls(), sta_date = month_ls()[0] + '-01', end_date = month_ls()[-1] + '-01', flag=0):
    df = get_IV_all(code, sta_date, end_date, flag)
    df_IV = df.groupby('date')['iv'].count().reset_index()
    def count_less_than(group):
        return (group['iv'] < 0.002).sum()
    df_IV_0002 = df.groupby('date').apply(count_less_than).reset_index().rename(columns={0:'无穷小值数'})
    df_all = df.groupby('date')['remaining_size'].count().reset_index()
    df_IV = pd.merge(df_IV, df_IV_0002, how = 'inner', on = 'date')
    df = pd.merge(df_IV, df_all, how = 'inner', on = 'date').rename(columns={'iv':'非空值数', 'remaining_size':'总数'})
    df['空值数'] = df['总数'] - df['非空值数']
    df['无效值数'] = df['空值数'] + df['无穷小值数']
    df['无效值占比'] = df['无效值数'] / df['总数']
    df = df[['date', '无效值占比']].rename(columns={'无效值占比':'IVnan_pct'})
    return df

# 数据存档
def IVnan_pct_save():
    excel_file_path = 'IVnan_pct.csv'
    #if os.path.exists('原始数据/' + excel_file_path):
    #    df = pd.read_csv('原始数据/' + excel_file_path)
    #    return df
    #else:
    save_CSV(NaN_pect(), 'get_data/原始数据/' + excel_file_path.split('.')[0])
    df = pd.read_csv('get_data/原始数据/' + excel_file_path)
    return df

